package com.happycat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happycat.base.BaseEntity;
import com.happycat.base.R;
import com.happycat.base.context.BaseContextHandler;
import com.happycat.base.exception.RestApiException;
import com.happycat.mapper.social.SocialFollowMapper;
import com.happycat.mobile.service.UserAccountService;
import com.happycat.social.dto.SocialFollowSaveDto;
import com.happycat.social.entity.SocialFollow;
import com.happycat.social.service.ISocialFollowService;
import com.happycat.social.vo.FollowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

@Service
@Transactional
public class SocialFollowServiceImpl extends ServiceImpl<SocialFollowMapper, SocialFollow> implements ISocialFollowService {

    @Autowired
    public SocialFollowMapper followMapper;
    @Autowired
    private UserAccountService userAccountService;

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public int addFollow(SocialFollowSaveDto dto) {
        //校验用户是否可用
        userAccountService.checkUserById(dto.getFollowedUser());
        SocialFollow follow = followMapper.selectOne(Wrappers.<SocialFollow>lambdaQuery()
                .eq(SocialFollow::getFollowedUser, dto.getFollowedUser())
                .eq(SocialFollow::getUser, BaseContextHandler.getUserId())
                .in((dto.getFocusType().intValue()==0 || dto.getFocusType().intValue()==3),
                        SocialFollow::getFocusType, 0, 3)
                .eq((dto.getFocusType().intValue()==1 || dto.getFocusType().intValue()==2),
                        SocialFollow::getFocusType, dto.getFocusType()));
        if(follow==null){
            //新增关注
            SocialFollow socialFollow = BeanUtil.copyProperties(dto, SocialFollow.class);
            socialFollow.setUser(BaseContextHandler.getUserId());
            if(save(socialFollow)){
                return 1;
            }else
                return 0;
        }else {
            if(dto.getFocusType().intValue() == follow.getFocusType().intValue()){
                throw new RestApiException(R.fail("您已添加"));
            }else {
                followMapper.update(follow, Wrappers.<SocialFollow>lambdaUpdate()
                        .set(SocialFollow::getFocusType, dto.getFocusType())
                        .eq(BaseEntity::getId, follow.getId()));
            }
            return 1;
        }
    }

    @Override
    public int cancelFollow(long followId) {
        SocialFollow follow = followMapper.selectById(followId);
        if(follow==null){
            throw new RestApiException(R.fail("未找到关注记录"));
        }
        return followMapper.deleteById(followId);
    }

    @Override
    public Page<FollowVo> myFollowPage(Page<FollowVo> page, int focusType) {
        page.setOrders(Arrays.asList(OrderItem.desc("f.create_time")));
//        Page<FollowVo> rsPage = followMapper.myFollowPage(page, Wrappers.<SocialFollow>lambdaQuery()
//                .eq(SocialFollow::getFocusType, 0)
//                .eq(SocialFollow::getUser, BaseContextHandler.getUserId()));
        Page<FollowVo> rsPage = followMapper.myFollowPage(page, BaseContextHandler.getUserId(), focusType);
        return rsPage;
    }

    @Override
    public Page<FollowVo> myFansPage(Page page) {
        page.setOrders(Arrays.asList(OrderItem.desc("f.create_time")));
        Page<FollowVo> rsPage = followMapper.myFansPage(page, BaseContextHandler.getUserId());
        return rsPage;
    }

    @Override
    public Page<FollowVo> myFriendsPage(Page page) {
        page.setOrders(Arrays.asList(OrderItem.desc("f.create_time")));
        Page<FollowVo> rsPage = followMapper.myFriendsPage(page, BaseContextHandler.getUserId());
        return rsPage;
    }

}